<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var a = 1
    var b = a
    console.log(b)

    a = 2
    console.log(b)


    var obj1 = {
      name: 'obj1',
      age: 28,
      // eat() {
      //   console.log(this);
      // }
      brother: {
        name: 'gege',
        wife: {

        }
      }
    }
    var obj2 = obj1
    console.log(obj2.name)

    // 克隆 （深拷贝、浅拷贝）

    // 浅克隆
    function clone1(target) {
      var res = {}

      // 遍历对象中的所有属性
      for (var key in target) {
        res[key] = target[key]
      }

      return res
    }

    var obj3 = clone1(obj1)
    // console.log(obj3);

    // obj1.name = 'ooooo'
    // console.log(obj3.name);

    // obj1.brother.name = 'didi'
    // console.log(obj3.brother.name);


    // 深克隆
    function clone2(target) {
      var res = {}

      // 遍历对象中的所有属性
      for (var key in target) {
        if (typeof target[key] == 'object') {
          res[key] = clone2(target[key])
        } else {
          res[key] = target[key]
        }
      }

      return res
    }

    var obj4 = clone2(obj1)
    obj1.brother.name = 'didi'
    console.log(obj4.brother.name);


  </script>
</body>

</html>